<?php

namespace App\Http\Controllers\Seller;

use App\Http\Requests\Seller\Goods\CreateRequest;
use App\Http\Requests\Seller\Goods\UpdateRequest;
use App\Models\Goods;
use App\Repositories\Seller\GoodsRepository;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    //
    public function __construct()
    {
        $this->repo = new GoodsRepository();
    }

    /**
     * 商品列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchList(Request $request)
    {
        $status = $request->get('status', -1);
        $goods_name = $request->get('goods_name', '');
        $type = $request->get('type', '');
        if ($type > 0) {
            $where['type_id'] = $type;
        }
        if ($status > -1) {
            $where['status'] = $status;
        }
        if ($goods_name) {
            $where[] = ['name', 'like', '%' . $goods_name . '%'];
        }
        $where['seller_id'] = $this->id();

        $param = $request->only('page', 'size');
        $param['with'] = ['goods_type'];
        $page = $this->repo->pagination($where, $param);
        return $this->success($page);
    }

    /**
     * 商品详情
     * @param Goods $goods
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchInfo(Goods $goods)
    {
        if ($goods->seller_id != $this->id()) {
            return $this->error('商品不存在', [], 404, 404);
        }
        $goods->load('sku');
        return $this->success($goods);
    }

    /**
     * 创建商品
     * @param CreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(CreateRequest $request)
    {
        $goods_data = $request->only('name', 'type_id', 'status', 'desc', 'thumb');
        $goods_data['seller_id'] = $this->id();
        $goods_data['desc'] = (isset($goods_data['desc']) && $goods_data['desc'] !== null) ? $goods_data['desc'] : "";
        $goods_data['thumb'] = (isset($goods_data['thumb']) && $goods_data['thumb'] !== null) ? $goods_data['thumb'] : "";
        $sku_data = $request->get('sku', []);
        $goods = $this->repo->create($goods_data, $sku_data);
        if (!$goods) {
            return $this->error('创建失败');
        }
        $goods->load('sku');
        return $this->success($goods, '创建成功');
    }

    /**
     * 更新商品
     * @param Goods $goods
     * @param UpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Goods $goods, UpdateRequest $request)
    {
        $goods_data = $request->only('name', 'type_id', 'status', 'desc', 'thumb');
        $goods_data['desc'] = (isset($goods_data['desc']) && $goods_data['desc'] !== null) ? $goods_data['desc'] : "";
        $goods_data['thumb'] = (isset($goods_data['thumb']) && $goods_data['thumb'] !== null) ? $goods_data['thumb'] : "";
        $sku_data = $request->get('sku', []);
        if ($goods->seller_id != $this->id()) {
            return $this->error('记录不存在', [], 404, 404);
        }
        $res = $this->repo->updateGoods($goods, [
            'goods' => $goods_data,
            'sku' => $sku_data
        ]);
        if (!$res) {
            return $this->error('更新失败');
        }
        $goods = $this->repo->model()->where(['id' => $goods->id])->first();
        if (!$goods) {
            return $this->error('数据错误');
        }
        $goods->load('sku');
        return $this->success($goods, '更新成功');
    }

    /**
     * @param Goods $goods
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStatus(Goods $goods)
    {
        if ($goods->seller_id != $this->id()) {
            return $this->error('记录不存在', [], 404, 404);
        }
        $data['status'] = !$goods->status;
        $this->repo->update($goods, $data);
        return $this->success([], '状态修改成功');
    }

    /**
     * @param Goods $goods
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delete(Goods $goods)
    {
        if ($goods->seller_id != $this->id()) {
            return $this->error('记录不存在', [], 404, 404);
        }
        $goods->delete();
        return $this->success([], '删除成功');
    }
}
